package juc.synchronize;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyLock {
    static int  count=1;
    static  Object o = new Object();
    public static void main(String[] args) {

        final boolean[] flag = {false};
        Thread t1 = new Thread(() -> {
            while (true) {
                synchronized (o) {
                    try {
                        Thread.sleep(1000);
                        log.info("第"+count++ +"次运行=================================");
                        log.debug("t1唤醒t2并进入等待");
                       o.notifyAll();
                        o.wait();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread t2 = new Thread(() -> {

            while (true) {
                synchronized (o) {
                    try {
                        Thread.sleep(1000);
                        log.debug("t2唤醒t1并进入等待");
                        o.notifyAll();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }

        });

        t1.start();
        t2.start();
    }
}
